/*
 * maz_app_debugcmd.h
 *
 *  Created on: 2020年6月14日
 *      Author: Paul
 */

#include "maz_com.h"

#ifndef INC_MAZ_APP_DEBUGCMD_H_
#define INC_MAZ_APP_DEBUGCMD_H_

#define MAZAPP_DEBUGCMD_TOTAL_LEN               64
#define MAZAPP_DEBUGCMD_IDLE_LEN(r, w, len)     ((w >= r) ? (len + r - w - 1) : (r - w - 1))
#define MAZAPP_DEBUGCMD_FILL_LEN(r, w, len)     ((w >= r) ? (w - r) : (len + w - r))

#define MAZAPP_DEBUGCMD_HISTORY_COUNT           (8)
#define MAZAPP_DEBUGCMD_HISTORY_PREV(n)         n = ((n >= MAZAPP_DEBUGCMD_HISTORY_COUNT - 1) ? 0 : (n - 1))
#define MAZAPP_DEBUGCMD_HISTORY_NEXT(n)         n = ((n >= MAZAPP_DEBUGCMD_HISTORY_COUNT - 1) ? 0 : (n + 1))

typedef struct _MAZAPP_DEBUGCMD_CFG_
{
    int reserved[4];
} MAZAPP_DEBUGCMD_CFG;

typedef struct _MAZAPP_DEBUGCMD_PRIV_
{
    /* 串口接收消息缓冲队列 */
    uint8_t buf[MAZAPP_DEBUGCMD_TOTAL_LEN];
    uint8_t r;
    uint8_t w;

    /* 历史命令暂存数组 */
    uint8_t history[MAZAPP_DEBUGCMD_HISTORY_COUNT][MAZCPNT_CMD_MAX_STR_LEN];
    int base;
    int offset;
} MAZAPP_DEBUGCMD_PRIV;

/* 环形队列 r = w 是空队列, w = r - 1 是满队列 */
typedef struct _MAZAPP_DEBUGCMD_CTRL_
{
    MAZAPP_DEBUGCMD_CFG cfg;
    MAZAPP_DEBUGCMD_PRIV priv;
} MAZAPP_DEBUGCMD_CTRL;

extern MAZAPP_DEBUGCMD_CTRL g_mazapp_debugcmd;

int MAZ_App_debugcmd_preinit(void);
int MAZ_App_debugcmd_init(void);
void MAZ_App_debugcmd_task(void *argument);
int MAZ_App_debugcmd_dump_queue(void);
int MAZ_App_debugcmd_dump_buff(void);

#endif /* INC_MAZ_APP_DEBUGCMD_H_ */
